/**
 * @author 董一华
 * @since 1.00
 * @version 1.00
 * 
 * 部门岗位下拉框控件
 * 使用说明：
 * jsp中使用范例(最短模式)：<htdz-tag:SelectDeptDutyTag orgacode="${xxx}"/>
 * 			   (最常用模式)：<htdz-tag:SelectDeptDutyTag orgacode="${xxx}" selected="${xxx}"/>
 * 参数说明：
 * 1. name可以不写,缺省值为"deptid_dutyid"
 * 2. id可以不写,缺省值为name的值    
 * 3. selected为下拉框选中的值：deptid#dutyid，可以不写。
 * 4. orgacode为用户所属组织，必填。
 * 
 * 修订列表： v1.00 2010-04-17 
 * 			修订人：董一华 
 * 			修订内容：初始版本
 * 
 */   

package com.zjht.gmcc.bean.tag;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MyParamUtil;

public class SelectDeptDutyTag extends TagSupport {
	private static final long serialVersionUID = 7343888033252888637L;
	private String name;
	private String id;
	private String selected;
	private String orgacode;

	public SelectDeptDutyTag() {
	}

	@SuppressWarnings("static-access")
	public int doStartTag() throws JspException {
		String name = this.name;
		String id = this.id;
		String selected = this.selected;
		String orgacode = this.orgacode;

		if (orgacode == null || orgacode.length() == 0) {
			throw new JspException("部门岗位下拉框orgacode不能为空！");
		}
		if (name == null || name.length() == 0) {
			name = "deptid_dutyid";
		}
		if (id == null || id.length() == 0) {
			id = name;
		}
		StringBuilder html = new StringBuilder(); 
		html.append("<select name=\"");
		html.append(name);
		html.append("\"	id=\"");
		html.append(id);
		html.append("\"><option value=\"");
		html.append(MyConstant.SELECT_FIRST_VALUE);
		html.append("\">请选择</option>");

		String channeltype = MyParamUtil.getChannelTypeByOrgaCode(orgacode);
		int orgatypeid = MyParamUtil.getOrgaTypeID(orgacode);

		if (channeltype != null && channeltype.length() > 0 && orgatypeid > 0) {
		//	boolean ismultiplat = MyParamUtil.isMultiPlatByOrgacode(orgacode); 
			MyJDBC myJDBC = null;
			try {
				myJDBC = new MyJDBC();
				String strSQL = "SELECT deptid,deptname,dutyid,dutyname FROM V_SYS_DEPT_DUTY_LIST WHERE orgatypeid=? AND (channeltype LIKE ? OR channeltype='all')  AND multiplat=?";
				PreparedStatement ps = myJDBC.prepareStatement(strSQL);
				ps.setInt(1, orgatypeid);
				ps.setString(2, "%" + channeltype + "%");
				ps.setString(3, "0"); 
				ResultSet rs = ps.executeQuery();
				String deptid_dutyid = "";
				while (rs.next()) {
					deptid_dutyid = rs.getString("deptid") + "#" + rs.getString("dutyid");
					html.append("<option value=\"");
					html.append(deptid_dutyid);
					html.append("\" ");
					if (deptid_dutyid.equals(selected))
						html.append("selected");
					html.append(">");
					html.append(rs.getString("deptname") + " - " + rs.getString("dutyname"));
					html.append("</option>");
				}
				rs.close();
				rs = null;
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				MyJDBC.close(myJDBC);
			}
		}
		html.append("</select>");
		try {
			pageContext.getOut().print(html.toString());
		} catch (Exception e) {
			throw new JspException(e.getMessage());
		}
		html=null;
		return this.SKIP_BODY;
	}

	public void setName(String newName) {
		name = newName;
	}

	public void setId(String newId) {
		id = newId;
	}

	public void setOrgacode(String orgacode) {
		this.orgacode = orgacode;
	}

	public void setSelected(String selected) {
		this.selected = selected;
	}
}
